package com.xumt.hwHMSdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.xumt.hwHMSdemo.cannectSb.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FinishedActivity extends AppCompatActivity {
    private SimpleAdapter finishadapter = null;
    private String finishlistresult;
    private List<Map<String, Object>> finishlist = new ArrayList<Map<String, Object>>();
    private String shopname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finished);
        finishadapter = new FinishedActivity.MySimpleAdapter(this, getData(), R.layout.finishlist, new String[]{"name","shopname", "price", "description","img"},
                new int[]{R.id.finishfood, R.id.finishshop,R.id.finishprice, R.id.finishdescription,R.id.finishimg});
        ListView listView = (ListView) findViewById(R.id.finishlist);
        listView.setAdapter(finishadapter);
        TextView textView=(TextView)findViewById(R.id.finishaddress);
        textView.setText(OrderActivity.address);
        Button button1 =(Button)findViewById(R.id.allback);//全部确认
        button1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                backall(OrderActivity.clickoderid);
            }
        });
    }
    //通过state查找订单
    private void findstate(int id) {
        new Thread() {
            public void run() {
                try {
                    HttpRequest request = new HttpRequest();
                   finishlistresult = request.sendGet("http://169.254.105.90:8081/Order2/getrelaid","relaid="+ id);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }
    //申请退单
    private void backall(int id) {
        new Thread() {
            public void run() {
                try {
                    HttpRequest request = new HttpRequest();
                    request.sendGet("http://169.254.105.90:8081/Order/modify2","orderid="+ id);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
    //单个退单
    private void back(int id) {
        new Thread() {
            public void run() {
                try {
                    HttpRequest request = new HttpRequest();
                    request.sendGet("http://169.254.105.90:8081/Order2/modify1","id="+ id+"&"+"relaid="+OrderActivity.clickoderid);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }
    //获取数据
    private List<Map<String, Object>> getData() {
        try {
            findstate(OrderActivity.clickoderid);
            Thread.sleep(500);
            JSONArray jsonArray = new JSONArray(finishlistresult);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("name", "商品名:" + jsonObject.getString("name"));
                map.put("price", "价格：" + jsonObject.getString("price"));
                map.put("id",  jsonObject.getInt("numid"));
                map.put("state",  jsonObject.getInt("state"));
                map.put("description", "简介：" + jsonObject.getString("description"));
                if(jsonObject.getInt("shopid")==1)
                {
                    shopname="zmhshop";
                }
                if(jsonObject.getInt("shopid")==2)
                {
                    shopname="开心宠物商店";
                }
                map.put("shopname", "店铺：" + shopname);
                Context ctx=getBaseContext();
                int resId = getResources().getIdentifier(jsonObject.getString("image"), "drawable" ,ctx.getPackageName());
                map.put("img",resId);
               finishlist.add(map);
            }
        } catch (JSONException | InterruptedException e) {
            e.printStackTrace();
        }
        return finishlist;
    }
    private class MySimpleAdapter extends SimpleAdapter {

        public MySimpleAdapter(Context context,
                               List<? extends Map<String, ?>> data, int resource,
                               String[] from, int[] to) {
            super(context, data, resource, from, to);
            // TODO Auto-generated constructor stub
        }

                @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            final int mPosition = position;
            convertView = super.getView(position, convertView, parent);

            Button buttonadd=(Button)convertView.findViewById(R.id.finishbutton);
            buttonadd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Map<String, Object>map=finishlist.get(mPosition);
                    if((int)map.get("state")==4) {
                        back((int) map.get("id"));
                        map.put("state",-2 );
                        finishlist.set(mPosition, map);//替换listItems中原来的map
                        finishadapter.notifyDataSetChanged();//通知SimpleAdapter数据改变
                        Toast.makeText(FinishedActivity.this, "商品退单成功", Toast.LENGTH_SHORT).show();
                    }
                    else{
                        Toast.makeText(FinishedActivity.this, "已是退单状态", Toast.LENGTH_SHORT).show();
                    }
                }
            });


            return convertView;
        }
    }
}